home *** CD-ROM | disk | FTP | other *** search
/ Experimental BBS Explossion 3 / Experimental BBS Explossion III.iso / c / lzw4c13.zip / RW_IO.C < prev    next >
Text File  |  1993-08-14  |  3KB  |  116 lines

  1. /*   RW_IO.C
  2. **
  3. **   Reader/Writer Buffered I/O
  4. **
  5. **   Reader() and Writer() are called directly by the LZW4C.ASM code.
  6. **   They should never be called by your application code.
  7. **
  8. **   The other functions are never called by the LZW4C.ASM code, but are
  9. *    called only by your application routines.
  10. **
  11. **   Note that only a Reader() and Writer() function is required by the
  12. **   LZW4C.ASM code. This means that you have complete control over data
  13. **   coming into and out of the compression/expansion code. Instead of
  14. **   reading or writing to disk, you can just as easily read/write to a
  15. **   buffer, serial port, etc. You just have to write the Reader() and
  16. **   Writer() code.
  17. */
  18.  
  19.  
  20. #include <stdio.h>
  21. #include "RW_IO.H"
  22.  
  23. #define FALSE 0
  24. #define TRUE !FALSE
  25.  
  26. #define BUFFER_SIZE 2048
  27.  
  28. typedef struct IOstruct
  29. {FILE *FilePtr;  /* file ptr */
  30.  char Buffer[BUFFER_SIZE];
  31.  int  Left;      /* leftmost byte in Buffer */
  32.  int  Right;     /* rightmost byte in buffer */
  33.  long Count;     /* # times Reader/Writer called */
  34. } IOstruct;
  35.  
  36. static IOstruct InpControl;
  37. static IOstruct OutControl;
  38.  
  39. int ReaderOpen(char *Ptr)
  40. {/* open input file */
  41.  InpControl.Left = 0;
  42.  InpControl.Right = 0;
  43.  InpControl.Count = 0;
  44.  InpControl.FilePtr = fopen(Ptr,"rb");
  45.  if(InpControl.FilePtr==NULL)
  46.    {printf("Cannot open '%s'\n",Ptr);
  47.     return(FALSE);
  48.    }
  49.  return(TRUE);
  50. }
  51.  
  52. int Reader(void)
  53. {char Byte;
  54.  if(InpControl.Left==InpControl.Right)
  55.     {/* read next buffer */
  56.      InpControl.Left = 0;
  57.      InpControl.Right = fread(&InpControl.Buffer,1,BUFFER_SIZE,InpControl.FilePtr);
  58.      if(InpControl.Right<=0) return(EOF);
  59.     }
  60.  /* return next byte */
  61.  Byte = InpControl.Buffer[InpControl.Left++];
  62.  InpControl.Count++;
  63.  return(0x00ff&Byte);
  64. }
  65.  
  66. long ReaderCount(void)
  67. {/* return bytes read */
  68.  return(InpControl.Count);
  69. }
  70.  
  71. void ReaderClose(void)
  72. {/* close input file */
  73.  fclose(InpControl.FilePtr);
  74. #if 0
  75. printf("%ld bytes read\n",InpControl.Count);
  76. #endif
  77. }
  78.  
  79. int WriterOpen(char *Ptr)
  80. {/* open output file */
  81.  OutControl.Left = 0;
  82.  OutControl.Right = 0;
  83.  OutControl.Count = 0;
  84.  OutControl.FilePtr = fopen(Ptr,"wb");
  85.  if(OutControl.FilePtr==NULL)
  86.    {printf("Cannot open '%s'\n",Ptr);
  87.     return(FALSE);
  88.    }
  89.  return(TRUE);
  90. }
  91.  
  92. int Writer(char Byte)
  93. {int Code;
  94.  OutControl.Count++;
  95.  if((OutControl.Count&0x0fff)==0) putchar('.');
  96.  OutControl.Buffer[OutControl.Right++] = Byte;
  97.  if(OutControl.Right==BUFFER_SIZE)
  98.     {/* read next buffer */
  99.      Code = fwrite(&OutControl.Buffer,1,OutControl.Right,OutControl.FilePtr);
  100.      OutControl.Right = 0;
  101.     }
  102.  return(Code);
  103. }
  104.  
  105. long WriterCount(void)
  106. {/* return bytes written */
  107.  return(OutControl.Count);
  108. }
  109.  
  110.  
  111. void WriterClose(void)
  112. {/* flush buffer to disk */
  113.  fwrite(&OutControl.Buffer[OutControl.Left],1,OutControl.Right-OutControl.Left,OutControl.FilePtr);
  114.  /* close output file */
  115.  fclose(OutControl.FilePtr);
  116. }